/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import 'package:flutter/material.dart';
import '../../l10n/generated/app_localizations.dart';

// 碰一碰首页
class KnockShareHomePage extends StatefulWidget {
  const KnockShareHomePage({super.key});

  @override
  State<KnockShareHomePage> createState() => _KnockShareHomePageState();
}

class _KnockShareHomePageState extends State<KnockShareHomePage>
    with SingleTickerProviderStateMixin {
  // 当前选中的按钮索引，-1表示未选中
  int _selectedIndex = -1;
  // 页面路由
  final List<String> _pageRoutes = [
    '/GalleryLayoutPage',
    '/ImmersiveCardPage',
    '/SplitCardPage'
  ];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // 获取本地化字符串
    AppLocalizations il8n = AppLocalizations.of(context)!;
    // 按钮文字
    final List<String> _buttonTexts = [
      il8n.galleryLayout,
      il8n.immersiveCardLayout,
      il8n.splitCardLayout
    ];
    return Scaffold(
      appBar: _buildAppBar(),
      resizeToAvoidBottomInset: true,
      backgroundColor: Colors.grey[100],
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            alignment: Alignment.centerLeft,
            margin: const EdgeInsets.only(left: 16),
            child: Text(
              il8n.knockShare,
              style: const TextStyle(
                  fontWeight: FontWeight.w700,
                  fontSize: 30,
                  color: Colors.black),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 16),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: _buttonTexts.asMap().entries.map((entry) {
                return _buildCustomButton(entry.key, _buttonTexts[entry.key]);
              }).toList(),
            ),
          )
        ],
      ),
    );
  }

  // 构建导航栏组件
  AppBar _buildAppBar() {
    return AppBar(
      elevation: 0,
      backgroundColor: Colors.grey[100],
      leading: IconButton(
          onPressed: () {
            Navigator.pop(context);
          },
          icon: Image.asset(
            'lib/assets/images/back.png',
            width: 40,
            height: 40,
          )),
    );
  }

  // 创建按钮
  Widget _buildCustomButton(int index, String buttonText) {
    final isSelected = _selectedIndex == index;
    return GestureDetector(
      onTap: () => _handleButtonTap(index),
      behavior: HitTestBehavior.translucent,
      child: Container(
        margin: const EdgeInsets.only(left: 16, right: 16, bottom: 12),
        height: 40,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: isSelected
                ? Color.fromRGBO(10, 89, 247, 1)
                : Color.fromRGBO(0, 0, 0, 0.05),
            borderRadius: BorderRadius.circular(20)),
        child: Text(
          buttonText,
          style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w600,
              color:
                  isSelected ? Colors.white : Color.fromRGBO(10, 89, 247, 1)),
        ),
      ),
    );
  }

  void _handleButtonTap(int index) {
    setState(() {
      if (_selectedIndex == index) {
        _selectedIndex = -1;
      } else {
        _selectedIndex = index;
      }
    });
    Navigator.pushNamed(context, _pageRoutes[index]);
  }
}
